//
//  HMEmoticonKeyBoardViewModel.swift
//  MyWeibo
//
//  Created by apple on 16/4/28.
//  Copyright © 2016年 apple. All rights reserved.
// 表情键盘所做的一些逻辑操作：读取表情，截取表情，保存最近表情

import UIKit


// 每一页有多少列
let HMEmoticonKetboardMaxCol = 7
// 每一页有多少行
let HMEmoticonKetboardMaxRow = 3
// 每一个页显示多少个表情按钮
let HMEmoticonKetboardPageEmticonCount = HMEmoticonKetboardMaxCol * HMEmoticonKetboardMaxRow - 1


class HMEmoticonKeyBoardViewModel: NSObject {
    
    // 创建一个单例
    static let sharedViewModel: HMEmoticonKeyBoardViewModel = HMEmoticonKeyBoardViewModel()

    // 表情的bundle
    lazy var emoticonBundle: NSBundle = {
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        return NSBundle(path: path)!
    }()
    
    // 定义一个数组记录默认表情  返回默认表情
    lazy var defaultEmoticons: [HMEmoticon] = {
        
        
        return self.emoticonsWithPath("default/info.plist")
    }()
    
    //返回emoji表情
    lazy var emojiEmoticons: [HMEmoticon] = {
        
        return self.emoticonsWithPath("emoji/info.plist")
    }()
    
    //返回浪小花
    lazy var lxhEmoticons: [HMEmoticon] = {
        
        return self.emoticonsWithPath("lxh/info.plist")
    }()
    
    
    //懒加载collextionview所需要的数组
    
    lazy var allEmoticons : [[[HMEmoticon]]] = {
        
        
        return [
            
            [[HMEmoticon]()],
            self.emoticonPageArray(self.defaultEmoticons),
            self.emoticonPageArray(self.emojiEmoticons),
            self.emoticonPageArray(self.lxhEmoticons),
        ]
    }()
    
    
    
    //切割数组  把数组按每页20 个来切割
    
    private func emoticonPageArray(emoticons:[HMEmoticon])->[[HMEmoticon]]{
        
        //求出表情有多少页
        let pageCount = (emoticons.count - 1) / 20 + 1
        
        //定义返回的二维数组
        var result:[[HMEmoticon]] = [[HMEmoticon]]()
        
        for i in 0..<pageCount{
            
            //计算截取开始位置
            let loc = i * 20
            
            //计算截取长度
            var len = 20
            
            //判断最后一页的截取长度
            if loc + len > emoticons.count {
                
                len = emoticons.count - loc
            }
            
            //按照开始位置和长度截取数组  转换成oc截取之后再转换回去
            let chlidArray = (emoticons as NSArray) .subarrayWithRange(NSMakeRange(loc, len)) as! [HMEmoticon]
            
            //添加到临时数组
            result.append(chlidArray)
            
        }
        
        
        return result
    }
    
    
    
    //创建方法返回不同路径下的表情包
    private func emoticonsWithPath(filePath : String)->[HMEmoticon]{
        
        
        // 1. 取到默认表情的info.plist路径
        let path = self.emoticonBundle.pathForResource(filePath, ofType: nil)!
        
        // 2. 读取字典数组
        let dicts = NSArray(contentsOfFile: path)!
        
        // 3. 初始化一个模型数组以保存模型
        var tempArray = [HMEmoticon]()
        
        // 4. 字典转模型
        for value in dicts {
            
            let model = HMEmoticon(dict: (value as! [String : AnyObject]))
            // 设置该表情对应的文件夹路径
            let floderName = (filePath as NSString).stringByDeletingLastPathComponent
            
            model.folderName = floderName
            
            tempArray.append(model)
        }
        
        // 5. 返回
        return tempArray
        
    }
    
    
}
